package com.restaurant.service.impl;

import com.restaurant.bean.Detail;
import com.restaurant.bean.User;
import com.restaurant.dao.DetailDao;
import com.restaurant.dao.ShopDao;
import com.restaurant.service.DetailService;
import com.restaurant.util.MyBatisUtils;
import com.restaurant.util.Result;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @program(包名): UOOrder
 * @description（类的作用）:
 * @author（作者）: 薛
 * @create: 2020-08-04 13:08
 */
public class DetailServiceImpl implements DetailService {
    private SqlSession sqlSession;
    private DetailDao detailDao;

    public DetailServiceImpl() {
        this.sqlSession = MyBatisUtils.getSqlSession();

        this.detailDao = sqlSession.getMapper(DetailDao.class);
    }

    /**
     * 根据详情id获取对应的订单详情
     *
     * @param did
     * @return 封装状态码，提示信息和详情对象的结果类
     */
    @Override
    public Result getDetail(int did) {
        Detail detail = detailDao.selectByDid(did);
        return Result.ok (200,"成功");
    }

    /**
     * 根据详情id删除订单详情，注意判读删除是否成功，以及提交事务
     *
     * @param did
     * @return 封装状态码，提示信息的结果类
     */
    @Override
    public Result removeDetail(int did) {
        int temp = detailDao.deleteDetail(did);
        sqlSession.commit();
        if (temp == 0) {
            return Result.ok(20, "添加失败");
        } else
            return Result.ok(200, "成功");

    }

    /**
     * 根据订单id查询订单详情(是一个list集合)
     *
     * @param oid
     * @return 封装状态码，提示信息和详情对象集合的结果类
     */
    @Override
    public Result getDetails(int oid) {
        List<Detail> details = detailDao.selectByOid(oid);
        if (details == null) {
            return Result.ok(100, "网络错误！");
        } else
            return Result.ok(200, "成功", details);
    }

    @Override
    public Result addDetail(Detail detail) {
        int i = detailDao.insertDetail(detail);
        if (i > 0){
            sqlSession.commit();
            return Result.ok(200,"添加成功");
        }
        return Result.ok(100,"网络故障，添加失败");
    }

    @Override
    public Result setDetailQuantity(int quantity, String goodsName) {
        int i = detailDao.updateCount(quantity, goodsName);
        if (i == 0){
            return Result.ok(100,"网络故障");
        }
        sqlSession.commit();
        return Result.ok(200,"修改成功");
    }

    @Override
    public Result setDetailQuantity(String goodsName) {
        return setDetailQuantity(0,goodsName);
    }

    @Override
    public Result listDetail() {
        List<Detail> details = detailDao.selectByQuantity(0);
        if (details == null || details.isEmpty()){
            return Result.ok(100,"订单为空");
        }
        return Result.ok(200,"查询成功",details);
    }

}

